package com.useeinfo.oa.modules.wuHanCommon.base.dao;


import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.inspection.entity.InspectionOrder;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.WarningCreateAccept;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("WarningCreateAcceptDao")
public class WarningCreateAcceptDao implements CrudDao<WarningCreateAccept> {
    private final static Logger logger = LoggerFactory.getLogger(WarningCreateAcceptDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();

        conditionHash.put("deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String projId = queryHash.get("projId");
        if (StringUtils.isNotBlank(projId)) {
            conditionHash.put("projId = ?{paramIndex} ", projId);
        }

        String errCode = queryHash.get("errCode");
        if (StringUtils.isNotBlank(errCode)) {
            conditionHash.put("errCode = ?{paramIndex} ", StringConverters.ToInteger(errCode));
        }

        String token = queryHash.get("token");
        if (StringUtils.isNotBlank(token)) {
            conditionHash.put("token = ?{paramIndex} ", token);
        }

        return conditionHash;
    }


    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(w) from WarningCreateAccept w ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<WarningCreateAccept> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select w from WarningCreateAccept w ", "order by w.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<WarningCreateAccept> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, WarningCreateAccept.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public WarningCreateAccept findModel(Long id) {
        return entityManager.find(WarningCreateAccept.class, id);
    }

    @Override
    public Integer add(WarningCreateAccept model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("WarningCreateAcceptDaoImpl添加WarningCreateAccept成功！");
        return 1;
    }

    @Override
    public Integer update(WarningCreateAccept model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        WarningCreateAccept warningCreateAccept = entityManager.find(WarningCreateAccept.class, id);
        entityManager.remove(warningCreateAccept);
        return 1;
    }

    @Override
    public Integer deleteLogic(WarningCreateAccept model) {
        return null;
    }

    public List<WarningCreateAccept> hqlExcute(String hql) {
        return entityManager.createQuery(hql, WarningCreateAccept.class).getResultList();
    }

    /**
     * 获取抄表的最后一次时间
     */
    public List<Object> getDate() {
        String sql = "SELECT DATE_FORMAT(create_date,'%Y-%m-%d %h:%i') FROM `wuhan_warning_create_accept` WHERE type=1 ORDER BY id DESC LIMIT 0,1";
        return entityManager.createNativeQuery(sql).getResultList();
    }
}
